//
//  TransactionDetailView.swift
//  bill
//
//  Created by Eudon on 2025/6/18.
//

import SwiftUI

// 交易详情视图
struct TransactionDetailView: View {
    var transaction: Transaction
    
    var body: some View {
        ScrollView {
            VStack(spacing: 24) {
                // 交易金额卡片
                VStack(spacing: 16) {
                    Text(transaction.type == .income ? "收入" : "支出")
                        .font(.system(size: 16, weight: .medium))
                        .foregroundColor(Color(.secondaryLabel))
                    
                    Text(transaction.type == .income ? "+¥\(transaction.amount, specifier: "%.2f")" : "-¥\(transaction.amount, specifier: "%.2f")")
                        .font(.system(size: 48, weight: .bold))
                        .foregroundColor(transaction.type == .income ? 
                                       Color(red: 0.3, green: 0.8, blue: 0.4) : 
                                       Color(red: 0.9, green: 0.3, blue: 0.3))
                }
                .padding(.horizontal, 24)
                .padding(.vertical, 32)
                .background(
                    RoundedRectangle(cornerRadius: 16)
                        .fill(Color(.systemBackground))
                        .shadow(color: Color.black.opacity(0.08), radius: 8, x: 0, y: 2)
                )
                .padding(.horizontal, 20)
                
                // 交易信息卡片
                VStack(spacing: 0) {
                    HStack {
                        Text("交易信息")
                            .font(.system(size: 18, weight: .semibold))
                            .foregroundColor(Color(.label))
                        Spacer()
                    }
                    .padding(.horizontal, 24)
                    .padding(.top, 24)
                    .padding(.bottom, 20)
                    
                    VStack(spacing: 0) {
                        // 类别
                        HStack {
                            Text("类别")
                                .font(.system(size: 16, weight: .medium))
                                .foregroundColor(Color(.label))
                            Spacer()
                            Text(transaction.category)
                                .font(.system(size: 16))
                                .foregroundColor(Color(.secondaryLabel))
                        }
                        .padding(.horizontal, 24)
                        .padding(.vertical, 16)
                        
                        Divider()
                            .padding(.horizontal, 24)
                        
                        // 类型
                        HStack {
                            Text("类型")
                                .font(.system(size: 16, weight: .medium))
                                .foregroundColor(Color(.label))
                            Spacer()
                            Text(transaction.type == .income ? "收入" : "支出")
                                .font(.system(size: 16))
                                .foregroundColor(transaction.type == .income ? 
                                               Color(red: 0.3, green: 0.8, blue: 0.4) : 
                                               Color(red: 0.9, green: 0.3, blue: 0.3))
                        }
                        .padding(.horizontal, 24)
                        .padding(.vertical, 16)
                        
                        Divider()
                            .padding(.horizontal, 24)
                        
                        // 日期
                        HStack {
                            Text("日期")
                                .font(.system(size: 16, weight: .medium))
                                .foregroundColor(Color(.label))
                            Spacer()
                            Text(transaction.date, style: .date)
                                .font(.system(size: 16))
                                .foregroundColor(Color(.secondaryLabel))
                        }
                        .padding(.horizontal, 24)
                        .padding(.vertical, 16)
                    }
                }
                .background(
                    RoundedRectangle(cornerRadius: 16)
                        .fill(Color(.systemBackground))
                        .shadow(color: Color.black.opacity(0.08), radius: 8, x: 0, y: 2)
                )
                .padding(.horizontal, 20)
                
                // 备注卡片（如果有备注）
                if let note = transaction.note, !note.isEmpty {
                    VStack(alignment: .leading, spacing: 16) {
                        Text("备注")
                            .font(.system(size: 18, weight: .semibold))
                            .foregroundColor(Color(.label))
                        
                        Text(note)
                            .font(.system(size: 16))
                            .foregroundColor(Color(.secondaryLabel))
                            .padding(.horizontal, 20)
                            .padding(.vertical, 16)
                            .frame(maxWidth: .infinity, alignment: .leading)
                            .background(
                                RoundedRectangle(cornerRadius: 12)
                                    .fill(Color(.tertiarySystemBackground))
                            )
                    }
                    .padding(.horizontal, 24)
                    .padding(.vertical, 24)
                    .background(
                        RoundedRectangle(cornerRadius: 16)
                            .fill(Color(.systemBackground))
                            .shadow(color: Color.black.opacity(0.08), radius: 8, x: 0, y: 2)
                    )
                    .padding(.horizontal, 20)
                }
            }
            .padding(.vertical, 20)
        }
        .background(Color(.systemGroupedBackground))
        .navigationTitle("交易详情")
        .navigationBarTitleDisplayMode(.large)
    }
}

#Preview {
    NavigationView {
        TransactionDetailView(transaction: Transaction(
            amount: 100.0,
            category: "餐饮",
            date: Date(),
            type: .expense,
            note: "午餐"
        ))
    }
} 